package com.ssmIntegration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssmIntegration.mapper.ScheduleMapper;
import com.ssmIntegration.pojo.Schedule;
import com.ssmIntegration.service.ScheduleService;
import com.ssmIntegration.utils.PageBean;
import com.ssmIntegration.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    public R page(int pageSize, int currentPage) {

        // 分页，在sql语句后面加上limit x,y
        PageHelper.startPage(currentPage, pageSize);

        // 查询
        List<Schedule> scheduleList = scheduleMapper.queryList();

        // 分页数据装配
        PageInfo<Schedule> info = new PageInfo<>(scheduleList);

        // 装配pageBean
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, info.getTotal(), info.getList());

        R ok = R.ok(pageBean);
        return ok;
    }

    @Override
    public R deleteById(int id) {
        int rows = scheduleMapper.deleteById(id);

        if (rows > 0) {
            return R.ok(null);
        }

        return R.fail(null);
    }

    @Override
    public R save(Schedule schedule) {
        int rows = scheduleMapper.insert(schedule);

        if (rows > 0) {
            return R.ok(null);
        }

        return R.fail(null);
    }

    @Override
    public R update(Schedule schedule) {
        // id不能为null
        if (schedule.getId() == null) {
            return R.fail("核心参数为null，不可修改！");
        }
        int rows = scheduleMapper.update(schedule);

        if (rows > 0) {
            return R.ok(null);
        }

        return R.fail(null);
    }


}
